package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Transaction;
import com.ruoyi.system.service.ITransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 月度 成交&预计成交Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/transaction")
public class TransactionController extends BaseController {
    @Autowired
    private ITransactionService transactionService;

    /**
     * 查询月度 成交&预计成交列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Transaction transaction) {
        startPage();
        List<Transaction> list = transactionService.selectTransactionList(transaction);
        return getDataTable(list);
    }

    @GetMapping("/info")
    public AjaxResult info(){
        Transaction transaction = new Transaction();
        List<Transaction> list = transactionService.selectTransactionList(transaction);
        Map<String, Object> map = new HashMap<>();
        List<String> name = new ArrayList<>();
        List<Integer> finish = new ArrayList<>();
        List<Integer> estimate = new ArrayList<>();
        for (Transaction transaction1 : list) {
            name.add(transaction1.getName());
            finish.add(transaction1.getFinish());
            estimate.add(transaction1.getEstimate());
        }
        map.put("name",name);
        map.put("finish",finish);
        map.put("estimate",estimate);
        return AjaxResult.success(map);
    }

    /**
     * 获取月度 成交&预计成交详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(transactionService.selectTransactionById(id));
    }

    /**
     * 新增月度 成交&预计成交
     */
    @PostMapping
    public AjaxResult add(@RequestBody Transaction transaction) {
        Transaction res = new Transaction();
        res.setName(transaction.getName());
        List<Transaction> list = transactionService.selectTransactionList(res);
        Assert.isTrue(list.size()==0,"已有该乡镇月度成交数据");
        transaction.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(transactionService.insertTransaction(transaction));
    }

    /**
     * 修改月度 成交&预计成交
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Transaction transaction) {
        transaction.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(transactionService.updateTransaction(transaction));
    }

    /**
     * 删除月度 成交&预计成交
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(transactionService.deleteTransactionByIds(ids));
    }
}
